using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.PlatForm.Dtos.DbLink;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 数据连接
    /// 版 本：V2.6.200509
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-11-29
    /// </summary>
    [Route("api/PlatForm/[controller]")]
    public class DbLinkController : BaseApiController
    {
        private DbLinkBll dbLinkBll = new DbLinkBll();
        private UserBll userBll = new UserBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(KeywordInput input)
        {
            var userData = await userBll.GetList();
            var data = await dbLinkBll.GetList();
            if (!string.IsNullOrEmpty(input.keyword))
            {
                data = data.FindAll(t => t.FullName.Contains(input.keyword) || t.Host.Contains(input.keyword));
            }
            var output = data.Select(async d => new DbLinkListOutput
            {
                creatorTime = d.CreatorTime,
                creatorUser = userBll.GetUserName(d.CreatorUserId),
                dbType = d.DbType,
                enabledMark = d.EnabledMark,
                fullName = d.FullName,
                host = d.Host,
                id = d.Id,
                lastModifyTime = d.LastModifyTime,
                lastModifyUser = userBll.GetUserName(d.LastModifyUserId),
                port = d.Port.ToString(),
                sortCode = d.SortCode
            }).ToList().Select(r => r.Result).ToList();
            return Success(new { list = output });
        }

        /// <summary>
        /// 下拉框列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("Selector")]
        public async Task<ResponseResult> GetSelector()
        {
            var data = (await dbLinkBll.GetList()).FindAll(m => m.EnabledMark == 1);
            var output = data.MapToList<DbLinkSelectorOutput>();
            return Success(new { list = output });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = await dbLinkBll.GetInfo(id);
            var output = data.MapTo<DbLinkInfoOutput>();
            return Success(output);
        }
        #endregion

        #region POST
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await dbLinkBll.GetInfo(id);
            if (entity != null)
            {
                await dbLinkBll.Delete(entity);
            }
            return Success("删除成功");
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] DbLinkCrInput input)
        {
            if (await dbLinkBll.IsExistByFullName(input.fullName, null))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<DbLinkEntity>();
            await dbLinkBll.Create(entity);
            return Success("创建成功");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] DbLinkUpInput input)
        {
            if (await dbLinkBll.IsExistByFullName(input.fullName, id))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<DbLinkEntity>();
            await dbLinkBll.Update(id, entity);
            return Success("更新成功", entity);
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost("Actions/Test")]
        public async Task<ResponseResult> TestDbConnection([FromBody] DbLinkActionsTestInput input)
        {
            var entity = input.MapTo<DbLinkEntity>();
            var data = await dbLinkBll.TestDbConnection(entity);
            if (data)
            {
                return Success("连接成功");
            }
            else
            {
                return Error("连接失败");
            }
        }
        #endregion
    }
}
